package com.x.compute.common.handle;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.x.compute.common.constant.StrConstant;
import com.x.compute.common.constant.VarTypeEnum;
import com.x.compute.common.entity.VarHandleResult;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 日期变量处理
 * @author xuxin
 */
@Slf4j
public class DateVarHandle implements VarTypeHandle {

    @Override
    public VarTypeEnum type() {
        return VarTypeEnum.DATE;
    }

    @Override
    public String formatExpression(String expression) {
        return expression;
    }

    @Override
    public VarHandleResult handle(Map<String, Object> fieldMap, String expression) {
        try {
            DateTime.of(expression, DatePattern.NORM_DATE_PATTERN);
            expression = StrUtil.wrap(expression, StrConstant.SINGLE_QUOTES);
        }catch (Exception e){
            log.error("计算公式{}无法转换日期格式", expression);
        }
        return VarHandleResult.builder().expression(expression).build();
    }
}
